import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

import ICart from 'src/types/cart.interface';

type UnionType = ICart | ICart[];


@Injectable()
export class CartService {

    /**
     * 参数装饰器,Cart 这个名字要和app.module.ts中的MongooseModule.forFeature 定义的名字一样
     * Cart 是定义好的
     * @param recommendModel 
     */
    constructor(@InjectModel('Cart') private cartModel: Model<ICart>) {

    }


    /**
     * 查询数据
     * @param queryCondition 
     * @returns 
     */
    async getAllData(): Promise<UnionType> {
        const querySelectFileds = ["productId","imageUrl","productName","description","price","count","selected","imageLink"];
        return this.cartModel.find().select(querySelectFileds).exec()
    }


    /***
     * 向mongodb中插入数据
    */
    async importData(requestParamsBody: UnionType): Promise<UnionType> {
        //如果请求体是一个数组
        if (Array.isArray(requestParamsBody)) {
            return this.cartModel.insertMany(requestParamsBody);
        }
        else {
            return new this.cartModel(requestParamsBody).save();
        }
    }


  
 
   


}





